package com.gamatechno.solodestination;

import com.gamatechno.solodestination.util.HttpUtils;

import java.net.URLEncoder;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SearchView;
import android.os.Build;

public class SearchActivity extends Activity {

	Button btnSearch;
	EditText txtInputan;
	String keyword, id , name;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search);
		Bitmap b = BitmapFactory.decodeResource(getResources(),
				R.drawable.header_img);
		BitmapDrawable bd = new BitmapDrawable(getResources(), b);
		ActionBar ab = getActionBar();
		ab.setBackgroundDrawable(bd);
		LayoutInflater inflater = (LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.actionbar_logo, null);
		ab.setCustomView(v);
		ab.setDisplayShowCustomEnabled(true);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setHomeButtonEnabled(true);
		getActionBar().setTitle("");
		getActionBar().setLogo(R.drawable.ic_home);

		if (savedInstanceState == null) {
			getFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
		
		btnSearch = (Button) findViewById(R.id.btnSearch);
		txtInputan = (EditText) findViewById(R.id.inputan);

		btnSearch.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// keyword = txtInputan.getText().toString();
				try {
					keyword = URLEncoder.encode(
							txtInputan.getText().toString(), "utf-8");
					new SearchTask().execute();

				} catch (Exception e) {
					Log.i("error_solo", e.getMessage());

				}
				
			}
		});

		
	}

	public class SearchTask extends AsyncTask<Void, Void, Void> {
		ProgressDialog dialog = new ProgressDialog(SearchActivity.this);

		protected void onPreExecute() {
			// dialog.setMessage(" Searching... ");
			// dialog.show();
		}

		@Override
		protected Void doInBackground(Void... params) {

			try {

				String url = "http://sms.gamatechno.com/solo_destination/services/getRef.php?"
						+ "method=search&format=json&keyword="
						+ keyword
						+ "&language=en";

				Log.i("url", url);

				HttpUtils u = new HttpUtils();
				String a = u.getContents(url);
				// String a = HttpUtils.getContents(url);
				Log.i("content", a);
				JSONObject obj = new JSONObject(a);
				Log.i("JSONObject content length", obj.length() + "");
				String jo = obj.get("result") + "";
				Log.i("isi result", jo + "");
				JSONArray ka = new JSONArray(jo);
				
				Log.i("result length", ka.length() + "");

				for (int i = 0; i < ka.length(); i++) {
					JSONObject ja = ka.getJSONObject(i);
					
					id = ja.getString("id");
					name = ja.getString("name");
					
				}
				
				
				
				
//				Log.i("result length", ka.length() + "");
				Log.i("id", id);
				Log.i("id", name);
//				

			} catch (Exception e) {

				// e.printStackTrace();
				Log.i("i", e.toString());
			}
			return null;

		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			Intent i = new Intent(SearchActivity.this, ListSearchActivity.class);
			i.putExtra("id", id);
			i.putExtra("name", name);
//			
			startActivity(i);
			
			super.onPostExecute(result);
		}

	}
	
	
	
	
	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			// View rootView = inflater.inflate(R.layout.fragment_search,
			// container, false);
			// return rootView;
			return null;
		}
	}

	// SEARCH ICON AND CLICKABLE
		

		@Override
		public boolean onOptionsItemSelected(MenuItem item) {
			// toggle nav drawer on selecting action bar app icon/title
			// Handle action bar actions click
			switch (item.getItemId()) {
			case R.id.action_search:
				return true;
			case android.R.id.home:
				this.finish();
				return true;
			default:
				return super.onOptionsItemSelected(item);
			}
		}

		
	
}
